package com.netease.financial.navigation;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.netease.financial.base.customerservice.CustomerServiceHelper;
import com.netease.financial.base.lockpattern.LockPatternHelper;
import com.netease.financial.base.lockpattern.LockPatternPreferencesHelper;
import com.netease.financial.base.login.LoginActivity;
import com.netease.financial.base.pdf.CloudPdfActivity;
import com.netease.financial.base.register.RegisterActivity;
import com.netease.financial.module.activitypage.ActivityPageActivity;
import com.netease.financial.module.authentication.AuthenticationActivity;
import com.netease.financial.module.main.MainActivity;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * Class used to navigate through the application.
 */
@Singleton
public class Navigator {

    @Inject
    public Navigator() {
        //empty
    }

    /**
     * Goes to the customer service screen.
     *
     * @param context A Context needed to open the destiny activity.
     */
    public void navigateToChatScreen(Context context) {
        if (context != null) {
            CustomerServiceHelper.openChatScreen(context);
        }
    }

    public void navigateToMainScreen(Context context) {
        if (context != null) {
            Intent intent = MainActivity.getCallingIntent(context);
            context.startActivity(intent);
        }
    }

    public void navigateToTab(Context context, int position) {
        if (context != null) {
            Intent intent = MainActivity.getCallingIntent(context, position);
            context.startActivity(intent);
        }
    }

    public void navigateToCreateLockPatternScreen(Activity activity, String userId, int requestCode) {
        if (activity != null && !TextUtils.isEmpty(userId)) {
            LockPatternPreferencesHelper.setFailedAttemptsSinceLastTimeout(0);
            LockPatternPreferencesHelper.setFailedAttemptTimeoutMillis(0);
            LockPatternHelper.showLockPatternCreateScreen(activity, userId, requestCode);
        }
    }

    public void navigateToCreateLockPatternScreen(Fragment fragment, String userId, int requestCode) {
        if (fragment != null && !TextUtils.isEmpty(userId)) {
            LockPatternHelper.showLockPatternCreateScreen(fragment, userId, requestCode);
        }
    }

    public void navigateToAuthenticationScreen(Activity activity, String clientId, String headPortraitUrl, String userId, String nextTip, int requestCode) {
        Intent intent = AuthenticationActivity.getCallingIntent(activity, clientId, headPortraitUrl, userId, nextTip);
        activity.startActivityForResult(intent, requestCode);
    }

    public void navigateToAuthenticationScreen(Fragment fragment, String clientId, String headPortraitUrl, String userId, String nextTip, int requestCode) {
        Intent intent = AuthenticationActivity.getCallingIntent(fragment.getContext(), clientId, headPortraitUrl, userId, nextTip);
        fragment.startActivityForResult(intent, requestCode);
    }

    public void loginWithOtherAccount(Context context) {
        LockPatternPreferencesHelper.setFailedAttemptsSinceLastTimeout(0);
        LockPatternPreferencesHelper.setFailedAttemptTimeoutMillis(0);
        Intent intent = MainActivity.getLoginWithAnotherAccountCallingIntent(context);
        context.startActivity(intent);
    }

    public void navigateToActivityPage(Context context, String pageTitle, String pageUrl) {
        Intent intent = ActivityPageActivity.getCallingIntent(context, pageTitle, pageUrl);
        context.startActivity(intent);
    }

    public void navigateToPreviewOnlinePdf(Context context, String pdfUrl) {
        Intent intent = CloudPdfActivity.getCallingIntent(context, pdfUrl);
        context.startActivity(intent);
    }

    public void navigateToPreviewOnlinePdf(Activity activity, String pdfUrl, int requestCode) {
        Intent intent = CloudPdfActivity.getCallingIntent(activity, pdfUrl);
        activity.startActivityForResult(intent, requestCode);
    }

    public void navigateToPreviewOnlinePdf(Fragment fragment, String pdfUrl, int requestCode) {
        Intent intent = CloudPdfActivity.getCallingIntent(fragment.getActivity(), pdfUrl);
        fragment.startActivityForResult(intent, requestCode);
    }

    public void navigateToNotificationDetailScreen(Context context) {
        Intent intent = MainActivity.getCallingIntent(context, MainActivity.TAB_POSITION_ASSET);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    public void navigateToLoginScreen(Activity activity, int requestCode) {
        Intent intent = LoginActivity.getCallingIntent(activity);
        activity.startActivityForResult(intent, requestCode);
    }

    public void navigateToLoginScreen(Fragment fragment, int requestCode) {
        Intent intent = LoginActivity.getCallingIntent(fragment.getActivity());
        fragment.startActivityForResult(intent, requestCode);
    }

    public void navigateToRegisterScreen(Activity activity, int requestCode) {
        Intent intent = RegisterActivity.getCallingIntent(activity);
        activity.startActivityForResult(intent, requestCode);
    }

    public void navigateToRegisterScreen(Fragment fragment, int requestCode) {
        Intent intent = RegisterActivity.getCallingIntent(fragment.getActivity());
        fragment.startActivityForResult(intent, requestCode);
    }

}
